package com.goldze.mvvmhabit.widght.expandswipe;

import android.util.SparseArray;

import androidx.databinding.ViewDataBinding;

import com.goldze.mvvmhabit.utils.Utils;

public class ExpandSwipeItemBinding<T> {
    private ExpandSwipeItemBind<T> itemBind;
    private int varibleId;
    private int layoutRes;
    private SparseArray<Object> extraBindings;

    public static <T> ExpandSwipeItemBinding<T> of(int varibleId, int layoutRes){
        return (new ExpandSwipeItemBinding(null)).set(varibleId,layoutRes);
    }

    public static <T> ExpandSwipeItemBinding<T> of(ExpandSwipeItemBind<T> itemBind){
        if(itemBind==null){
            throw new NullPointerException("itemBind is null");
        }else{
            return new ExpandSwipeItemBinding<>(itemBind);
        }
    }
    public final ExpandSwipeItemBinding<T> set(int varibleId, int layoutRes) {
        this.varibleId = varibleId;
        this.layoutRes = layoutRes;
        return this;
    }

    public  ExpandSwipeItemBinding(ExpandSwipeItemBind<T> itemBind) {
        this.itemBind = itemBind;
    }

    public final ExpandSwipeItemBinding varibleId(int varibleId) {
        this.varibleId = varibleId;
        return this;
    }

    public final ExpandSwipeItemBinding layoutRes(int layoutRes) {
        this.layoutRes = layoutRes;
        return this;
    }

    public final int getVaribleId() {
        return varibleId;
    }

    public final int getLayoutRes() {
        return layoutRes;
    }

    public final ExpandSwipeItemBinding bindExtra(int varibleId,Object o){
        if(this.extraBindings==null){
            this.extraBindings = new SparseArray<>(1);
        }
        extraBindings.put(varibleId,o);
        return this;
    }

    public final ExpandSwipeItemBinding removeExtra(int varibleId,Object o){
        if(this.extraBindings!=null){
          extraBindings.remove(varibleId);
        }
        return this;
    }

    public final ExpandSwipeItemBinding clearExtra(){
        if(this.extraBindings!=null){
            this.extraBindings.clear();
        }
        return this;
    }
    public final Object getExtra(int varibleId){
        return this.extraBindings==null?null:this.extraBindings.get(varibleId);
    }

    public void onItemBind(int position,T item){
        if(this.itemBind!=null){
            this.varibleId = -1;
            this.layoutRes = 0;
            this.itemBind.setItemBind(this,position,item);
            if(varibleId==-1){
                throw new IllegalStateException("variableId not set in onItemBind()");
            }
            if(layoutRes==0){
                throw new IllegalStateException("layoutRes not set in onItemBind()");
            }
        }
    }
    public void onItemBind(int position,int childPosition,T item){
        if (this.itemBind != null) {
            this.varibleId = -1;
            this.layoutRes = 0;
            this.itemBind.setItemBind(this, position, item);
            if (this.varibleId == -1) {
                throw new IllegalStateException("variableId not set in onItemBind()");
            }

            if (this.layoutRes == 0) {
                throw new IllegalStateException("layoutRes not set in onItemBind()");
            }
        }
    }

    public boolean Bind(ViewDataBinding binding, T item){
        {
            if (this.varibleId == 0) {
                return false;
            } else {
                boolean result = binding.setVariable(this.varibleId, item);
                if (!result) {
                    Utils.throwMissingVariable(binding, this.varibleId, this.layoutRes);
                }

                if (this.extraBindings != null) {
                    int i = 0;

                    for(int size = this.extraBindings.size(); i < size; ++i) {
                        int variableId = this.extraBindings.keyAt(i);
                        Object value = this.extraBindings.valueAt(i);
                        if (variableId != 0) {
                            binding.setVariable(variableId, value);
                        }
                    }
                }

                return true;
            }
        }
    }
}
